var live_id = 0;
var jump_url = '';
var room_name = '';
$('.live_room').on('click', function(){

	if(buying){
		return;
	}
	buying = true;
	// $('#pwd_box').show();
	jump_url = $(this).find('.jump_live').data('url');
	room_name = $(this).find('.room_name').html();
	console.log(jump_url)
	
	live_id = $(this).data('live_id');
	console.log(live_id);
	$.ajax({
		url:'/FrontChannel/check_live_pay',
		type:'post',
		data:{live_id:live_id},
		success:function(r){
			if(r.code == 1){
				toast(r.msg, 'fail');
			}else if(r.code == 2){
				$('.balance').find('span').html(r.left_money + '元');
				if(isios == 1){//ios充值
					native_listen('ios_recharge', {});
				}else{
					$('#recharge').show();
				}
			}else if(r.code == 3){console.log(jump_url)
				native_listen('jump_live',{
					url:jump_url,
					room_name:room_name,
				})
			}else{
				var npw_pay = check_npw(r.price);
				console.log(npw_pay)
				//是否开启免密
				if(npw_pay){
					live_buy(live_id);
				}else{
					$('#pwd_box').show();
					buying = false;
				}
			}
		}
	})

	// var url = $(this).data('url');
	// console.log(url)
	// native_listen('jump_live',{
	// 	url:url,
	// })
})
//微信和支付宝
		$(".pay").on("click",function(){

	  		$(this).find('.paymethod').addClass("on");
	  		$(this).find("input").prop("checked",true);
	  		$(this).siblings("div").children("input").attr("checked","false");
	  		$(this).siblings("div").children("span").removeClass("on");
	  		var val_payPlatform = $('.choose_pay input[name="pay_method"]:checked ').val();
	  		console.log(val_payPlatform);
  		})
		//购买-充值
		// $('.buy_btn').on('click',function(){
		// 	$('#recharge').show();
		// })
		//选择金额
  		$('.money').on('click',function(){
  			recharge_num = $(this).data('money')
  			$(this).addClass('active').siblings().removeClass('active');
  		})	
		//充值-关闭
  		$('#recharge .close_recharge').on('click',function(){
  			$('#recharge').hide();
  			buying = false;
  			// $('.zdy_money').html('').attr('data-money', '');
		   //  $('.money').removeClass('active');
		   //  $('.customize').val('').show();
		   //  $('.zdy_money').hide();
  		})
  		//充值
  		$('#chongzhi').on('click', function(){
  			recharge_num = $('.recharge_pop').find('.btn_group').find('.active').attr('data-money');
  			console.log('recharge_num:'+recharge_num)
  			$('.amount').html(recharge_num+'元');
  			$('#recharge').hide();
  			$('#pay_box').show();
  		})
		//支付关闭
  		$('#pay_box .close_pay').on('click',function(){
  			$('#pay_box').hide();
  			buying = false;
  		})
  		//支付按钮
  		$('#zhifu').on('click',function(){
  			// alert('支付');
  			var payway = $('[name="pay_method"]:checked').val()
  			// recharge_num = $('.recharge_pop').find('.btn_group').find('.active').data('money');

  			console.log(payway);
  			console.log(recharge_num);

  			$.ajax({
				url:'/FrontUserCenter/payment',
				type:'post',
				data:{recharge_num:recharge_num, payway:payway},
				success:function(r){
					console.log(r);
					if(r.code == 0){
						native_listen(payway,{
						    parameter : r.parameter,
						});
					}else{
						layer.open({
			                content: '支付失败',
			                title: false,
			                btn: ['确定'],
			            });
					}
					buying = false;
				}
			})


  		})
  		//密码
  		$('#pwd_ipt').on('input',function(){

  			var len = $('#pwd_ipt').val().trim().length;
  			console.log(len);
  			for(var i=0;i<len;i++){
				$(".dot").eq(i).addClass('show');

			}
			for(var j=len;j<6;j++){
				$(".dot").eq(j).removeClass('show');
			}

			if(len===6){
				if(buying){
					return;
				}
				buying = true;
				var pwd = $(this).val();
				
				setTimeout(function(){
					$.ajax({
						url:'/FrontChannel/check_pwd',
						type:'post',
						data:{pwd:pwd},
						success:function(r){
							if(r.code == 1){
								toast(r.msg, 'fail');
								$('#pwd_ipt').val('');
								$('.dot').removeClass('show');
								buying = false;
							}else{
								live_buy(live_id);
								$('#pwd_box').hide();
					  			$('#pwd_ipt').val('');
					  			$('.dot').removeClass('show');
							}
						}
					})
				},300)

			}

  		})
  		//关闭密码
  		$('.close_pwd').on('click',function(){
  			$('#pwd_box').hide();
  			$('#pwd_ipt').val('');
  			$('.dot').removeClass('show');
  		})
  		
  		$('.customize').blur(function(){
		    var price = $(this).val();
		    
		    if(price != ''){
		        $('.zdy_money').html(price+'元');
		        $('.zdy_money').attr('data-money', price);
		        $('.money').removeClass('active');
		        $('.customize').hide();
		        $('.zdy_money').addClass('active').show();
		        recharge_num = price;
		        console.log(recharge_num)
		    }else{
		    	$('.money').eq(0).addClass('active');
		    }
		})

		$('.zdy_money').on('click', function(){
		    $(this).hide();
		    $('.customize').show().focus();
		})
  		
	
	
	function check_npw(price){
		var npw = false;
		$.ajax({
			url:'/FrontChannel/check_npw_pay',
			data:{price:price},
			type:'post',
			async:false,
			success:function(r){
				if(parseInt(r) == 1){
					npw = true;
				}
			}
		})
		return npw;
	}


function live_buy(live_id){
	$.ajax({
		url:'/FrontChannel/live_buy',
		type:'post',
		data:{live_id:live_id},
		// async:false,
		beforeSend:function(){
			$('#loading').show()
		},
		success:function(r){
			$('#loading').hide();
			if(r.code == 0){console.log(jump_url)
				toast(r.msg, 'succ');
				setTimeout(function(){

					buying = false;
					// location.reload();
					native_listen('jump_live',{
						url:jump_url,
						room_name:room_name,
					})
				}, 2000);
			}else{
				toast(r.msg, 'fail');
			}
		}
	})
}